import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%load_ext watermark
%watermark --author "Ryan Sloot |" -d -v
## W-->sunny, rainy, snowy | I-->1,0
prob_W_I = np.array([[1/2, 0], [0, 1/6], [0,1/3]])
## Marginal distributions
## of p_W and p_I
prob_W = prob_W_I.sum(axis=1)
prob_I = prob_W_I.sum(axis=0)
prob_I
np.outer(prob_W, prob_I)==prob_W_I
## not independent
prob_X_Y = np.array([[1/4,1/4], [1/12,1/12],[1/6,1/6]])
prob_X = prob_X_Y.sum(axis=1)
prob_Y = prob_X_Y.sum(axis=0)
np.outer(prob_X, prob_Y) == prob_X_Y
.1/.35
.4/.65
8/13
P_S_C = np.array([[0.4, 0.1], [.25, .25]])
P_S = P_S_C.sum(axis=1)#rows
P_C = P_S_C.sum(axis=0)#cols
np.outer(P_S, P_C) == P_S_C
p_s_c_given_T0 = np.array([[0.72, 0.08], [0.18, 0.02]])
p_s_c_given_T1 = np.array([[0.08, 0.12], [0.32, 0.48]])
p_s_givenT0 = p_s_c_given_T0.sum(axis=1)
p_s_givenT1 = p_s_c_given_T1.sum(axis=1)
p_c_givenT0 = p_s_c_given_T0.sum(axis=0)
p_c_givenT1 = p_s_c_given_T1.sum(axis=0)
np.outer(p_s_givenT0, p_c_givenT0) == p_s_c_given_T0
##hmmm
p_s_c_given_T0
Find $P_T(t)$
Using total probability to marginalize for t=0:
$\qquad P(s=0, c=0) = P(s=0,\ c=0\ |\ t=0)\cdot P(t=0) + P(s=0,\ c=0\ |\ t=1)\cdot P(t=1)$
can replace $P(t=1)$ with $1-P(t=0)$
$P(s=0,\ c=0)$
print('%.2f'%P_S_C[0,0])
$P(s=0,\ c=0 \ |\ t=0)$:
## given t=0, prob s=0,c=0
print('%.2f' % p_s_c_given_T0[0,0])
right now we have:
$\qquad0.40 = 0.72\cdot P(t=0) + P(s=0,\ c=0\ |\ t=1)\cdot(1-P(t=0))$
finding: $P(s=0\ ,c=0\ |\ t=1)$:
print('%.2f'% p_s_c_given_T1[0,0])
Now solving:
$\qquad 0.40=0.72\cdot P(t=0) + 0.08 - 0.08\cdot P(t=0)$
##Solve
P_T = {}
P_T[0] = (0.40-0.08)/(0.72-0.08)
P_T[1] = 1 - P_T[0]
P_T
## simulating 1000 die rolls
import random
rolls = {i:0 for i in range(1,7)}
random.seed(1)
for _ in range(1000):
roll = random.randint(1,6)
rolls[roll] += 1
rolls
expected_val = np.array([k*v for k,v in rolls.items()]).sum() / 1000
expected_val
Variance using expectations:
$\qquad var(X)=\mathbb{E} [X^2]- (\mathbb{E} [X])^2$
L1 = {-1: 999999/1000000, 999: 1/1000000}
L2 = {-1: 999999/1000000, 999999: 1/1000000}
L3 = {-1: 9/10, 9: 1/10}
## L1
import math
e_x2 = np.array([k**2*v for k,v in L1.items()]).sum()
e_x_2 = np.array([k*v for k,v in L1.items()]).sum()**2
sd_l1 = math.sqrt(e_x2-e_x_2)
e_x2 - e_x_2
## L2
e_x2 = np.array([k**2*v for k,v in L2.items()]).sum()
e_x_2 = np.array([k*v for k,v in L2.items()]).sum()**2
sd_l2 = math.sqrt(e_x2-e_x_2)
e_x2 - e_x_2
## L3
e_x2 = np.array([k**2*v for k,v in L3.items()]).sum()
e_x_2 = np.array([k*v for k,v in L3.items()]).sum()**2
sd_l3 = math.sqrt(e_x2-e_x_2)
e_x2 - e_x_2
print('Standard Deivations:\nL1: %f\nL2: %f\nL3: %f\n' % (sd_l1,sd_l2,sd_l3))